-- InstType
-- 1 = 10man (25man prewrath)
-- 2 = 25man or 5man heroic (wrath)

function WhisperRaidInfo_OnLoad()

    --this:RegisterEvent("ADDON_LOADED");
    WhisperRaidInfo:RegisterEvent("CHAT_MSG_WHISPER");



    SLASH_WRI1 = "/whisperraidinfo";
    SLASH_WRI2 = "/wri"; -- shortcut
    SlashCmdList["WRI"] = WRI_SlashHandler;

    --DEFAULT_CHAT_FRAME:AddMessage("WhisperRaidInfo Loaded");

end

function WhisperRaidInfo_OnEvent(event, arg1, arg2)
    if(event=="CHAT_MSG_WHISPER") then
        if WRI_GetCmd(arg1) == "!raidinfo" then
            --DEFAULT_CHAT_FRAME:AddMessage("WRI: Raid Info sent to " .. arg2 .. ".");
            WRI_RaidInfo("whisper", arg2);
        end
   end
end

function WRI_ChatSend(WRI_ChatMessage, WRI_target, WRI_whispertar)
    if WRI_target == "guild" then
        SendChatMessage(WRI_ChatMessage, GUILD);
    elseif WRI_target == "self" then
        DEFAULT_CHAT_FRAME:AddMessage(WRI_ChatMessage);
    elseif WRI_target == "say" then
        SendChatMessage(WRI_ChatMessage, SAY);
    elseif WRI_target == "whisper" then
        SendChatMessage(WRI_ChatMessage, WHISPER, nil, WRI_whispertar);
    elseif WRI_target == "party" then
        SendChatMessage(WRI_ChatMessage, PARTY);
    elseif WRI_target == "raid" then
        SendChatMessage(WRI_ChatMessage, RAID);
    end
end

-- Info Handler
function WRI_RaidInfo(WRI_target, WRI_whispertar)
        WRI_InstNum = GetNumSavedInstances();
        WRI_ChatSend("WRI: Currently saved to " .. WRI_InstNum .. " instance(s).", WRI_target, WRI_whispertar);
        WRI_showloop = 0;
        while WRI_showloop < WRI_InstNum do
            WRI_showloop = WRI_showloop + 1;
            WRI_name, WRI_ID, WRI_remaining, WRI_difficulty = GetSavedInstanceInfo(WRI_showloop);
            if WRI_difficulty == 1 then
                WRI_diffText = " (Normal) ";
            elseif WRI_difficulty == 2 then
                WRI_diffText = " (Heroic) ";
            else
                WRI_diffText = " (ERR: Unknown) ";
            end

            WRI_remaining = WRI_remaining / 60 / 60;
            WRI_unit = " Hours Remaining ";

            if WRI_remaining > 24 then
                WRI_remaining = WRI_remaining / 24;
                WRI_unit = " Days Remaining ";
            end

            WRI_remaining = WRIround(WRI_remaining, 1)

            WRI_ChatSend("WRI: " .. WRI_showloop .. ") " .. " " .. WRI_name .. WRI_diffText .. " ID: " .. WRI_ID .. " " .. WRI_remaining .. WRI_unit, WRI_target, WRI_whispertar);
        end
end

function WRIround(num, idp)
  local mult = 10^(idp or 0)
  return math.floor(num * mult + 0.5) / mult
end


local function WRI_WhispFilter(msg, event, ...)
  local message = ...
  if WRI_GetCmd(message) == "!raidinfo" then
    return true, ...
  end
  if WRI_GetCmd(message) == "WRI:" and event == "CHAT_MSG_WHISPER_INFORM" then
    return true, ...
  end
  return false, ...
end


    ChatFrame_AddMessageEventFilter("CHAT_MSG_WHISPER_INFORM", WRI_WhispFilter)
    ChatFrame_AddMessageEventFilter("CHAT_MSG_WHISPER", WRI_WhispFilter)


-- Ghetto CMD Parser
 function WRI_GetCmd(msg)
 	if msg then
 		local a,b,c=strfind(msg, "(%S+)"); --contiguous string of non-space characters
 		if a then
 			return c, strsub(msg, b+2);
 		else
 			return "";
 		end
 	end
 end

-- Main Slash Handler
 function WRI_SlashHandler(WRI_cmd)
    --DEFAULT_CHAT_FRAME:AddMessage("WRI Command: " .. WRI_cmd);
    WRI_cmd2, WRI_cmd3 = WRI_GetCmd(WRI_cmd)
    --DEFAULT_CHAT_FRAME:AddMessage("WRI Main Command: " .. WRI_cmd2);
    --DEFAULT_CHAT_FRAME:AddMessage("WRI Sub Command: " .. WRI_cmd3);
    if WRI_cmd2 == "guild" then
        WRI_RaidInfo("guild");
    elseif WRI_cmd2 == "whisper" then
        WRI_RaidInfo("whisper", WRI_cmd3);
    elseif WRI_cmd2 == "say" then
        WRI_RaidInfo("say");
    elseif WRI_cmd2 == "party" then
        WRI_RaidInfo("party");
    elseif WRI_cmd2 == "raid" then
        WRI_RaidInfo("raid");
    elseif WRI_cmd2 == "about" then
        DEFAULT_CHAT_FRAME:AddMessage("Whisper Raid Info 0.1 alpha by Thomas Prescott");
    else
        WRI_RaidInfo("self");
    end
end


